package org.mirrentools.attendance.console.router;

import org.mirrentools.attendance.console.service.RecordService;

import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.jdbc.JDBCClient;
import io.vertx.ext.sql.assist.SQLExecute;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;

/**
 * Record的接口服务
 *
 * @author
 */
public class RecordRouter {
  /**
   * 日志工具
   */
  private final Logger LOG = LoggerFactory.getLogger(this.getClass());
  /**
   * 数据服务接口
   */
  private RecordService service;

  private RecordRouter(RecordService service) {
    super();
    this.service = service;
  }

  /**
   * 启动服务
   *
   * @param router  Router
   * @param execute 数据执行器
   */
  public static void startService(Router router, SQLExecute<JDBCClient> execute) {
    RecordRouter instance = new RecordRouter(RecordService.create(execute));
    // 获取行事历分类
    router.route("/api/record/findCalendarGroup").handler(instance::findCalendarGroup);
    // 获取行事历与用户的考勤状态
    router.route("/api/record/findCalendar").handler(instance::findCalendar);
    // 获取每小时记录
    router.route("/api/record/findDays").handler(instance::findDays);
    // 获取每天记录
    router.route("/api/record/findMonths").handler(instance::findMonths);
    // 获取考勤记录
    router.route("/api/record/findCounts").handler(instance::findCounts);
    // 获取最后一次考勤
    router.route("/api/record/getLastWork").handler(instance::getLastWork);
    // 获取工作
    router.route("/api/record/work").handler(instance::work);
    // 获取休息
    router.route("/api/record/rest").handler(instance::rest);
    // 上传图片
    router.route("/api/record/upload/image").handler(instance::uploadImage);

  }

  private void findCalendarGroup(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.findCalendarGroup]-->请求参数:\n" + params);
    }
    service.findCalendarGroup(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }

  private void findCalendar(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.findCalendar]-->请求参数:\n" + params);
    }
    service.findCalendar(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }

  private void findDays(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.findDays]-->请求参数:\n" + params);
    }
    service.findDays(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }

  private void findMonths(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.findMonths]-->请求参数:\n" + params);
    }
    service.findMonths(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }

  private void findCounts(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.findCounts]-->请求参数:\n" + params);
    }
    service.findCounts(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }

  private void getLastWork(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.getLastWork]-->请求参数:\n" + params);
    }
    service.getLastWork(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }

  private void work(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.work]-->请求参数:\n" + params);
    }
    service.work(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }

  private void rest(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.work]-->请求参数:\n" + params);
    }
    service.rest(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }

  private void uploadImage(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[RecordService.work]-->请求参数:\n" + params);
    }
    service.uploadImage(rct, params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }
}
